package com.ruoyi.business.component.mqconsumer.repaymentconsumers;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bizsys.domain.RepaymentOrder;
import com.ruoyi.bizsys.mapper.RepaymentOrderMapper;
import com.ruoyi.bizsys.utils.BizConstants;
import com.ruoyi.business.service.IPaystackRepaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 *  余额代扣消费
 * @author wqg
 * @description
 * @date 2022/5/21
 */
@Slf4j
@Service(BizConstants.ROCKMQ_REPAYMENT_TYPE_CARD_WITHHOLD)
public class PaystackCardWithholdConsumer implements IRepaymentMqService{

    @Autowired
    IPaystackRepaymentService iPaystackRepaymentService;
    @Autowired
    RepaymentOrderMapper repaymentOrderMapper;

    /**
     * jsonParam.put("iou",iou);
     * jsonParam.put("data",data);
     * @param msgObj
     */
    @Override
    public void handle(JSONObject msgObj) {
        log.info("余额代扣消费:data:{}", msgObj.toString());
        String repaymentNo = msgObj.getString("repaymentNo");
        RepaymentOrder repaymentOrder = repaymentOrderMapper.getDetail(repaymentNo);
        if(null!=repaymentOrder && repaymentOrder.getRepayStatus()==BizConstants.REPAYMENT_ORDER_STATUS_SUCCESS){
            log.info("repaymentOrder.getRepayStatus():"+repaymentOrder.getRepayStatus());
            return;
        }

        // 解析res 更新金额，金额需要除以100
        repaymentOrder.setRepaymentAmt(msgObj.getJSONObject("data").getBigDecimal("amount").divide(new BigDecimal("100")));
        repaymentOrderMapper.updateById(repaymentOrder);

        iPaystackRepaymentService.repaymentForCardWithhold(repaymentOrder.getPayTransSerialNo(), msgObj.getJSONObject("data"));
        log.info("余额代扣消费:：data:{}",msgObj);

    }
}
